#include <stdio.h>
#include "sdkconfig.h"

#include "esp_heap_caps.h"
#include "esp_spi_flash.h"
#include "esp_system.h"

#include "lvgl_port.h"
#include "driver/gpio.h"
#include "esp_log.h"
#include "audio.h"
#include "nvs_flash.h"
#include "esp_spiffs.h"
#include "i2c_bus2.h"
#include "music.h"
#include "gpio.h"
#include "color.h"
#include "main.h"
#include "storage.h"
#include "wifi.h"
#include "wifi_scan.h"
#include "wifi_scan_page.h"
#include "main_page.h"
#include "lv_symbol_extra_def.h"
#include "weather_page.h"
#include "lvgl_port.h"
#include "lv_port_fs.h"
#include "music_page.h"
#include "task_mag.h"
#include "pwm.h"
#include "lwip/err.h"
#include "lwip/sockets.h"
#include "lwip/sys.h"
#include "lwip/netdb.h"
#include "lwip/dns.h"
#include "cJSON.h"
#include "strings.h"
#include "http_time.h"
#include "udp.h"
#include "virtual_key_page.h"

#define TAG "VIRTUAL_KEY_PAGE"

xQueueHandle virtual_key_page_queue;
extern xQueueHandle main_page_queue;
extern xQueueHandle udp_send_queue;

static void display_title(uint8_t sw)
{
    static uint32_t init = false;
    static lv_obj_t *lv_label;

    lv_port_sem_take();

    if (sw)
    {
        if (init == false)
        {
            init = true;

            lv_label = lv_label_create(lv_scr_act(), NULL);
            lv_label_set_text(lv_label, "Virtual key");
            lv_obj_set_style_local_text_color(lv_label, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, COLOR_THEME);
            lv_obj_set_style_local_text_font(lv_label, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, &font_en_bold_48);
            lv_obj_align(lv_label, NULL, LV_ALIGN_CENTER, -10, 65);
        }
    }
    else
    {
        if (init == true)
        {
            init = false;

            lv_obj_del(lv_label);
        }
    }

    lv_port_sem_give();
}

/**
 *
 * @brief 页面任务
 *
 */
void virtual_key_page_task()
{
    uint32_t page_option_data;

    while (1)
    {
        if (xQueueReceive(virtual_key_page_queue, &page_option_data, portMAX_DELAY))
        {
            switch (page_option_data)
            {
            case PAGE_SHOW:
                display_title(true);
                break;
            case PAGE_EXIT:
                display_title(false);
                vTaskDelay(200 / portTICK_RATE_MS);
                page_option_data = PAGE_SHOW;
                xQueueSend(main_page_queue, &page_option_data, NULL);

                cql_page_dele(VIRTUAL_KEY_PAGE_TASK_NUM);
                break;
            case PAGE_LEFT_KEY: // ctrl
                ESP_LOGE(TAG, "收到键值 左");

                break;
            case PAGE_MID_KEY: // c
                ESP_LOGE(TAG, "收到键值 中");

                if (gpio_get_level(KEYBOARD_1) == 0)
                {
                    ESP_LOGE(TAG, "收到键值 Ctrl + C");
                    xQueueSend(udp_send_queue, "{key:\"Ctrl + C\"}", NULL);
                }

                if (gpio_get_level(KEYBOARD_2) == 0 && gpio_get_level(KEYBOARD_3) == 0)
                {
                    page_option_data = PAGE_EXIT;
                    xQueueSend(virtual_key_page_queue, &page_option_data, NULL);
                }
                break;
            case PAGE_RIGHT_KEY: // v
                ESP_LOGE(TAG, "收到键值 右");

                if (gpio_get_level(KEYBOARD_1) == 0)
                {
                    ESP_LOGE(TAG, "收到键值 Ctrl + V");
                    xQueueSend(udp_send_queue, "{key:\"Ctrl + V\"}", NULL);
                }

                if (gpio_get_level(KEYBOARD_2) == 0 && gpio_get_level(KEYBOARD_3) == 0)
                {
                    page_option_data = PAGE_EXIT;
                    xQueueSend(virtual_key_page_queue, &page_option_data, NULL);
                }
                break;
            default:

                break;
            }
        }
    }
}
